package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashSet;
import java.util.Set;

@RunWith(SpringRunner.class)
@SpringBootTest
public class OpForZset {
    @Autowired
    public RedisTemplate redisTemplate;
    @Test
    public void zset1(){
        ZSetOperations.TypedTuple<Object> objectTypedTuple1=new DefaultTypedTuple<>("zset-1",9.96);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2=new DefaultTypedTuple<>("zset-2",9.9);
        Set<ZSetOperations.TypedTuple<Object>> tuples=new HashSet<ZSetOperations.TypedTuple<Object>>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        System.out.println(redisTemplate.opsForZSet().add("zset1",tuples));
        System.out.println(redisTemplate.opsForZSet().range("zset1",0,-1));

    }
    @Test
    public void zset2(){
        System.out.println(redisTemplate.opsForZSet().add("zset3","zset-1",1.0));
        System.out.println(redisTemplate.opsForZSet().add("zset3","zset-1",1.0));
    }
    @Test
    public void zset3(){
        System.out.println(redisTemplate.opsForZSet().add("zset3","zset-1",1.0));
        System.out.println(redisTemplate.opsForZSet().add("zset3","zset-2",1.0));
        System.out.println(redisTemplate.opsForZSet().range("zset3",0,-1));
        System.out.println(redisTemplate.opsForZSet().remove("zset3","zset-2"));
        System.out.println(redisTemplate.opsForZSet().range("zset3",0,-1));
    }
    @Test
    public void zset4(){
        System.out.println(redisTemplate.opsForZSet().add("zset4","zset-1",1.0));
        System.out.println(redisTemplate.opsForZSet().add("zset4","zset-2",2.0));
        System.out.println(redisTemplate.opsForZSet().range("zset4",0,-1));
        System.out.println(redisTemplate.opsForZSet().rank("zset4","zset-2"));

    }
    @Test
    public void zset5(){
        ZSetOperations.TypedTuple<Object> objectTypedTuple1=new DefaultTypedTuple<>("zset-1",9.6);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2=new DefaultTypedTuple<>("zset-2",9.1);
        Set<ZSetOperations.TypedTuple<Object>> tuples=new HashSet<ZSetOperations.TypedTuple<Object>>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        System.out.println(redisTemplate.opsForZSet().add("zset5",tuples));
        System.out.println(redisTemplate.opsForZSet().range("zset5",0,-1));

    }
    @Test
    public void zset6(){
        ZSetOperations.TypedTuple<Object> objectTypedTuple1=new DefaultTypedTuple<>("zset-1",3.6);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2=new DefaultTypedTuple<>("zset-2",4.1);
        ZSetOperations.TypedTuple<Object> objectTypedTuple3=new DefaultTypedTuple<>("zset-3",5.7);
        Set<ZSetOperations.TypedTuple<Object>> tuples=new HashSet<ZSetOperations.TypedTuple<Object>>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        tuples.add(objectTypedTuple3);
        System.out.println(redisTemplate.opsForZSet().add("zset6",tuples));
        System.out.println(redisTemplate.opsForZSet().rangeByScore("zset6",0,9));
        System.out.println(redisTemplate.opsForZSet().count("zset6",0,5));

    }
    @Test
    public void zset7(){
        ZSetOperations.TypedTuple<Object> objectTypedTuple1=new DefaultTypedTuple<>("zset-1",3.6);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2=new DefaultTypedTuple<>("zset-2",4.1);
        ZSetOperations.TypedTuple<Object> objectTypedTuple3=new DefaultTypedTuple<>("zset-3",5.7);
        Set<ZSetOperations.TypedTuple<Object>> tuples=new HashSet<ZSetOperations.TypedTuple<Object>>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        tuples.add(objectTypedTuple3);
        System.out.println(redisTemplate.opsForZSet().add("zset7",tuples));
        System.out.println(redisTemplate.opsForZSet().size("zset7"));
    }
    @Test
    public void zset8(){
        ZSetOperations.TypedTuple<Object> objectTypedTuple1=new DefaultTypedTuple<>("zset-1",3.6);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2=new DefaultTypedTuple<>("zset-2",4.1);
        ZSetOperations.TypedTuple<Object> objectTypedTuple3=new DefaultTypedTuple<>("zset-3",5.7);
        Set<ZSetOperations.TypedTuple<Object>> tuples=new HashSet<ZSetOperations.TypedTuple<Object>>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        tuples.add(objectTypedTuple3);
        System.out.println(redisTemplate.opsForZSet().add("zset8",tuples));
        System.out.println(redisTemplate.opsForZSet().score("zset8","zset-2"));
    }
    @Test
    public void zset9(){
        ZSetOperations.TypedTuple<Object> objectTypedTuple1=new DefaultTypedTuple<>("zset-1",3.6);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2=new DefaultTypedTuple<>("zset-2",4.1);
        ZSetOperations.TypedTuple<Object> objectTypedTuple3=new DefaultTypedTuple<>("zset-3",5.7);
        Set<ZSetOperations.TypedTuple<Object>> tuples=new HashSet<ZSetOperations.TypedTuple<Object>>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        tuples.add(objectTypedTuple3);
        System.out.println(redisTemplate.opsForZSet().add("zset9",tuples));
        System.out.println(redisTemplate.opsForZSet().range("zset9",0,-1));
        System.out.println(redisTemplate.opsForZSet().removeRange("zset9",1,2));
        System.out.println(redisTemplate.opsForZSet().range("zset9",0,-1));

    }
    @Test
    public void zset10(){
        ZSetOperations.TypedTuple<Object> objectTypedTuple1=new DefaultTypedTuple<>("zset-1",3.6);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2=new DefaultTypedTuple<>("zset-2",5.1);
        ZSetOperations.TypedTuple<Object> objectTypedTuple3=new DefaultTypedTuple<>("zset-3",2.7);
        Set<ZSetOperations.TypedTuple<Object>> tuples=new HashSet<ZSetOperations.TypedTuple<Object>>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        tuples.add(objectTypedTuple3);
        System.out.println(redisTemplate.opsForZSet().add("zset10",tuples));
        Cursor<ZSetOperations.TypedTuple<Object>> cursor=redisTemplate.opsForZSet().scan("zset10", ScanOptions.NONE);
        while (cursor.hasNext()){
            ZSetOperations.TypedTuple<Object> item=cursor.next();
            System.out.println(item.getValue()+":"+item.getScore());
        }

    }


}
